<?php 
/*
Plugin Name: Seta interest
*/
class Seta_interest_Widget extends WP_Widget{

  public function __construct() {
        parent::__construct(false,$name = __('Seta Interest Info'));
  }
  public function widget( $args,$instanse ){

    if(isset($instanse['show_post_widget_title'])){
        $widget_title = $instanse['show_post_widget_title'];
    }

    if(isset($instanse['show_post_widget_cat_value'])){
        $cat_id = $instanse['show_post_widget_cat_value'];
    }

    if(isset($instanse['num_post'])){
        $num_post = $instanse['num_post'];
    }
?>
<!-- output html -->
<div id="workWithUs" class="region text-center animated-area">
  <div class="container">
    <h2 class="title-big">Làm việc cùng chúng tôi</h2>
    <div class="row">
    <?php
      $args = array('posts_per_page' => $num_post,'cat' => $cat_id);
      query_posts($args);
      while(have_posts()) : the_post();
    ?>
      <div class="col-lg-4">
        <div class="animated" data-animation="fadeInDown">
          <?php echo the_post_thumbnail(); ?>
        </div>
        <h3 class="title"><?php the_title(); ?></h3>
        <p><?php the_content(); ?></p>
        <p><a href="<?php echo get_permalink(); ?>" class="btn btn-primary" role="button">Xem thêm</a></p>
      </div>
    <?php
      endwhile;
    ?>
    </div>
  </div>
</div>
<?php 
  }
  public function form( $instanse ){

    $instance = wp_parse_args( (array) $instance, $defaults );

    if(isset($instanse['show_post_widget_cat_value'])){
        $show_post_widget_cat_value = $instanse['show_post_widget_cat_value'];
    }
    
    if(isset($instanse['show_post_widget_title'])){
        $show_post_widget_title = $instanse['show_post_widget_title'];
    }
    
    if(isset($instanse['num_post'])){
        $num_post = $instanse['num_post'];
    }
?>
<!-- Widget form -->
<p>Title:<input type="text" name="<?php echo $this->get_field_name('show_post_widget_title'); ?>" value="<?php echo $show_post_widget_title; ?> " /></p>
<p>Select category :
  <?php
    wp_dropdown_categories(array(
      'name' => $this->get_field_name('show_post_widget_cat_value'),
      'id' => $this->get_field_name('show_post_widget_cat_id'),
      'hide_empty' => 0,
      'class' => 'show_post_widget_category',
      'selected' => $show_post_widget_cat_value
    ))
   ?>
</p>
<p>Number post :
  <select name="<?php echo $this->get_field_name('num_post') ?>" id="<?php echo $this->get_field_id('num_post') ?>" class="number_post">
    <option value="1" <?php if($num_post == 1 ){ echo 'selected'; } ?> class="level-0">1</option>
    <option value="2" <?php if($num_post == 2){ echo 'selected'; } ?> class="level-0">2</option>
    <option value="3" <?php if($num_post == 3){ echo 'selected'; } ?> class="level-0">3</option>
    <option value="4" <?php if($num_post == 4){ echo 'selected'; } ?> class="level-0">4</option>
    <option value="5" <?php if($num_post == 5){ echo 'selected'; } ?> class="level-0">5</option>
  </select>
</p>
<?php    
  }
  public function update($new_instance,$old_instance){
    $instance = $old_instance;
      
    $instance['show_post_widget_cat_value'] = (!empty($new_instance['show_post_widget_cat_value'])) ? 
    strip_tags($new_instance['show_post_widget_cat_value']) : '';
    
    $instance['show_post_widget_title'] = (!empty($new_instance['show_post_widget_title'])) ? 
    strip_tags($new_instance['show_post_widget_title']) : '';
    
    $instance['num_post'] = (!empty($new_instance['num_post'])) ? 
    strip_tags($new_instance['num_post']) : '';
    
    return $instance;
  }
}
add_action('widgets_init',function(){register_widget('Seta_interest_Widget');});